﻿

CREATE PROCEDURE [dbo].[exbug_GenereazaPACsiAB]
	-- Add the parameters for the stored procedure here
	@IDDocument bigint,
	@CodUnitate smallint,
	@IDPAC int OUT,
	@IDAB int OUT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @IDResponsabil int
	SET @IDResponsabil = 0
	
	SELECT TOP 1 @IDResponsabil = [IDResponsabil] FROM ResponsabilVizaCFP WHERE [CodUnitate] = @CodUnitate
	IF @IDResponsabil = 0
		GOTO Eroare

	--Propunere angajare cheltuiala
    INSERT INTO [PropunereAngajareCheltuiala]
           ([Numar]
           ,[Data]
           ,[Compartiment]
           ,[Scopul]
           ,[Beneficiarul]
           ,[CodUnitate]
           ,[SumaTotala]
           ,[VizatCFP]
           ,[MotivRefuz]
           ,[IDResponsabil]
           ,[DataVizaCFP]
           ,[NrRegCFP]
           ,[OrdonatorCredite])
     SELECT CAST(NumarDoc as varchar(20)) [Numar],
		[DataDoc],
		[CompartimentSpecialitate],
		COALESCE([Observatii],''),
		[Linia1Stanga],
		rd.[CodUnitate],
		ValoareLei,
		1,'',@IDResponsabil,GETDATE(),'',[OrdonatorCredite]
      FROM [RulajeDoc] rd JOIN ExBugSetup ebs ON rd.CodUnitate = ebs.CodUnitate
       JOIN BalconSetup bcs ON bcs.CodUnitate = rd.CodUnitate
			 WHERE [IDDocument]=@IDDocument 
           
	SET @IDPAC= SCOPE_IDENTITY()

	INSERT INTO [PropunereAngajareCheltuialaDetaliu]
           ([IDPac]
           ,[IDJurnal]
           ,[ArtAl]
           ,[Suma]
           ,[CredBugAprob]
           ,[CredBugAngaj]
           ,[Clasif])
     SELECT @IDPAC,sd.IDJurnal,dbo.GetArticolAliniat(ContDebit) ArtAl ,
     CASE WHEN rl.ContDebit like ContAB +'%' THEN rl.valoareLei ELSE  -rl.valoareLei END,0,0,'' FROM 
     Rulaje rl JOIN [RulajeDoc] sd ON sd.IDDocument=rl.IDDocument
     JOIN ExBugSetup ebs ON ebs.CodUnitate = rl.CodUnitate
		WHERE rl.[IDDocument]=@IDDocument
		AND ((rl.ContDebit like ContAB +'%') OR (rl.ContCredit like ContAB +'%'))

	

	--Angajament bugetar
    INSERT INTO [AngajamentBugetar]
           ([Numar]
           ,[Data]
           ,[Compartiment]
           ,[Scopul]
           ,[Beneficiarul]
           ,[CodUnitate]
           ,[SumaTotala]
           ,[VizatCFP]
           ,[MotivRefuz]
           ,[IDResponsabil]
           ,[DataVizaCFP]
           ,[NrRegCFP]
           ,[OrdonatorCredite],
           [TipAngajament])
     SELECT CAST(NumarDoc as varchar(20)) [Numar],
		[DataDoc],
		[CompartimentSpecialitate],
		COALESCE([Observatii],''),
		[Linia1Stanga],
		rd.[CodUnitate],
		[ValoareLei],
		1,'',@IDResponsabil,GETDATE(),'',[OrdonatorCredite],0
      FROM [RulajeDoc] rd JOIN ExBugSetup ebs ON rd.CodUnitate = ebs.CodUnitate
       JOIN BalconSetup bcs ON bcs.CodUnitate = rd.CodUnitate
			 WHERE [IDDocument]=@IDDocument 
           
	SET @IDAB= SCOPE_IDENTITY()

	INSERT INTO [AngajamentBugetarDetaliu]
           ([IDAB]
           ,[IDJurnal]
           ,[ArtAl]
           ,[Suma]
           ,[Clasif])
     SELECT @IDAB,sd.IDJurnal,dbo.GetArticolAliniat(ContDebit) ArtAl ,
     rl.valoareLei,'' FROM 
     Rulaje rl JOIN [RulajeDoc] sd ON sd.IDDocument=rl.IDDocument
     JOIN ExBugSetup ebs ON ebs.CodUnitate = rl.CodUnitate
		WHERE rl.[IDDocument]=@IDDocument
		AND ((rl.ContDebit like ContAB +'%') OR (rl.ContCredit like ContAB +'%'))

		
		         
	GOTO Gata
Eroare:
	 RAISERROR ('Nu a fost definit responsabilul pentru viza CFP1',16,-1)
Gata:

END